<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="PositionFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="PositionFormData.smsName" placeholder="请输入广告位置名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        align="center"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            align="center"
                            sortable
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="type"
                            label="广告类型"
                            align="center"
                            sortable
                            width="180">
                    </el-table-column>

                    <el-table-column
                            prop="smsName"
                            label="广告位置名称"
                            sortable
                            align="center"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="descrition"
                            label="广告说明"
                            align="center"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="maxCount"
                            label="广告数量"
                            align="center"
                            sortable
                            width="180">
                    </el-table-column>
                    <el-table-column label="操作" width="210" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>


            </template>
        </el-card>



        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="20%"
                :before-close="handleClose"
        >
            <!--            model:表单数据-->
            <el-form  label-width="100px" :model="PositionForm" :rules="formRules" ref="formRef">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="PositionForm.id" ></el-input>
                </el-form-item>
                <el-form-item label="广告类型" prop="type" :error="existError" >
                    <el-input v-model="PositionForm.type"  @blur="nameUnique" ></el-input>
                    <!--<el-select v-model="PositionForm.type" placeholder="请选择广告类型">
                        <el-option
                            v-for="itemes in typeArray"
                            :key="itemes.id"
                            :label="itemes.type"
                            :value="itemes.id">
                        </el-option>
                    </el-select>-->
                </el-form-item>
                <el-form-item label="位置名称" prop="smsName"  >
                    <el-input v-model="PositionForm.smsName"></el-input>
                </el-form-item>
                <el-form-item label="广告说明" >
                    <el-input v-model="PositionForm.descrition"></el-input>
                </el-form-item>
                <el-form-item label="广告数量" >
                    <el-input v-model.number="PositionForm.maxCount"></el-input>
                </el-form-item>


            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="reset">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>



    </div>
</template>

<script>
    export default {
        name: "Position",
        data(){
            let checkRepeat = (rule, value, callback) => {
                this.$http.post(`/marketing/position/nameunique`, this.AdvertiseForm).then(res=> {
                    console.log(res);
                    if (res.data.code = 5000) {
                        callback(res.data.message)
                    } else {
                    }
                })
            }
            return{
                editDialogVisible:false,
                //条件搜索对象
                PositionFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                flag:true,
                //位置的查询表单
                PositionForm:{},
                // typeArray:[{id:0,type:"文体广告"},{id:1,type:"商品广告"},{id:2,type:"形象广告"}],
                //错误信息提示
                existError:"",
                //表单校验
                formRules: {
                    smsName:[{required:true,message:"请输入位置名称",trigger:'blur'}],
                    type:[{required:true,message:"请输入类型名称",trigger:'blur'},
                        /*{validator: checkRepeat, trigger: 'blur'}*/
                    ],
                }

            }
        },
        //表单加载时就执行
        created() {
            this.selectall();
        },
        methods:{
            formSubmit(){
                //提交增加和修改的表单校验
                var that=this;
                this.$refs.formRef.validate((valid)=>{
                    if (valid&&that.flag) {
                        if (this.PositionForm.id != null) {
                            this.$http.put("marketing/position/updateposition", this.PositionForm).then(function (result) {
                                if(result.data.code===2000){
                                    that.editDialogVisible = false;
                                    that.selectall();
                                }else{
                                    that.message("修改失败");
                                    that.selectall();
                                    that.PositionForm={};
                                }


                            })

                        } else {
                            this.$http.post("marketing/position/insertposition", this.PositionForm).then(function (result) {
                                if(result.data.code===2000){
                                    that.editDialogVisible = false;
                                    that.selectall();
                                }

                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
            //查询所有
            selectall(){
                var that=this;
                this.$http.post(`marketing/position/selectpage/${this.currentPage}/${this.pageSize}`,this.PositionFormData).then(function (returnResult) {
                    that.total=returnResult.data.result.total;
                    that.tableData=returnResult.data.result.records;

                })
            },
            chaxun(){
                var that=this;
                this.$http.get(`marketing/position/selectall`,this.PositionFormData).then(function () {
                    that.selectall()
                })
                /*var that=this;
                that.selectall();*/
            },
            //打开弹出框的弹出层
            insert(){
                this.editDialogVisible=true;
                this.PositionForm={};
            },
            //删除
            deleteById(id){
                var that=this;
                this.$http.delete("marketing/position/deleteposition?id="+id).then(function () {
                    that.selectall();
                    that.$message.success("删除成功");
                })
            },
            //通过id查找回显
            selectById(id){
                var that=this;
                this.editDialogVisible=true;
                this.$http.get(`marketing/position/selectbyid/${id}`).then(function (returnResult) {
                    that.PositionForm=returnResult.data.result.previews;
                    console.log(returnResult)
                })
            },
            //当前页条数
            handleSizeChange(v){
                this.pageSize=v;
                this.selectall();
            },
            //当前页
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectall();
            },
            //点击取消按钮可以消除警告文字
            reset(){
                this.editDialogVisible=false;
                this.$refs.CustomerIssuesForm.resetFields();
            },
            handleClose(){
                this.editDialogVisible=false;
                this.$refs.formRef.resetFields();
            },
            //广告名唯一性校验
            nameUnique(){
                var that = this;
                that.flag=true;
                this.$http.post(`/marketing/position/nameunique`, this.PositionForm).then(function (result) {
                    if(result.data.code!=2000){
                        that.flag=false;
                    }
                    that.existError = result.data.message;
                });


            },





        }
    }
</script>

<style scoped>

</style>